package com.kingyea.app.market.applicationType.service.impl;

import com.alibaba.fastjson.JSON;
import com.kingyea.app.market.applicationType.bean.ApplicationType;
import com.kingyea.app.market.applicationType.dao.ApplicationTypeMapper;
import com.kingyea.app.market.applicationType.service.ApplicationTypeService;
import com.kingyea.app.market.utils.page.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version v1.0.0
 * @description ApplicationType 实现类
 * @date Created in 2018-02-10 16:14:42
 */
@Service
@Transactional
public class ApplicationTypeServiceImpl implements ApplicationTypeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationTypeServiceImpl.class);

    @Resource
    private ApplicationTypeMapper applicationTypeMapper;

    /**
     * 通过ID查找对象
     *
     * @param classCode
     * @return
     */
    @Override
    public ApplicationType getById(Integer classCode) throws Exception {
        return this.applicationTypeMapper.getById(classCode);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public Object list(Map<String, Object> applicationType) throws Exception {
        List<String> marketCodeListParam = new ArrayList<>();
        if (applicationType.containsKey("marketCodeList") && !StringUtils.isEmpty(applicationType.get("marketCodeList"))) {
            marketCodeListParam = JSON.parseObject(JSON.toJSONString(applicationType.get("marketCodeList")), List.class);
        }
        applicationType.remove("marketCodeList");
        applicationType.put("marketCodeList", marketCodeListParam);
        PageInfo.startPage(applicationType);
        return PageInfo.getPageInfo(applicationTypeMapper.list(applicationType));
    }

    /**
     * 删除
     *
     * @param classCode
     * @return
     */
    @Override
    public int deleteById(String classCode) throws Exception {
        applicationTypeMapper.deleteById(classCode);
        return 1;
    }

    /**
     * 保存
     *
     * @param applicationType
     * @return
     */
    @Override
    public Object save(Map<String, Object> applicationType) throws Exception {
        try {
            applicationTypeMapper.save(applicationType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return applicationType;
    }

    /**
     * 更新
     *
     * @param applicationType
     * @return
     */
    @Override
    public ApplicationType update(ApplicationType applicationType) throws Exception {
        try {
            applicationTypeMapper.update(applicationType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return applicationType;
    }

    @Override
    public List<ApplicationType> getByMarket(String marketCode) throws Exception {
        // TODO Auto-generated method stub
        List<ApplicationType> byMarket = applicationTypeMapper.getByMarketCode(marketCode);
        return byMarket;
    }


}
